package com.bangdao.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;

import com.bangdao.entity.Oss;
import com.bangdao.mapper.OssMapper;
import com.bangdao.service.FileService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    /**
     * 数据库读取配置信息
     */
    @Autowired
    private OssMapper ossMapper;

    String endPoint = null;
    String keyId = null;
    String keySecret = null;
    String bucketName = null;

    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {
        List<Oss> ossList = ossMapper.selectList(null);
        for (Oss oss : ossList) {
            endPoint = oss.getEndpoint();
            keyId = oss.getAccessKeyId();
            keySecret = oss.getAccessKey();
            bucketName = oss.getBucketName();
        }

        //判断oss实例是否存在：如果不存在则创建，如果存在则获取
        OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
        if (!ossClient.doesBucketExist(bucketName)) {
            //创建bucket
            ossClient.createBucket(bucketName);
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }
        //构建日期路径：avatar/2019/02/26/文件名
        String folder = new DateTime().toString("yyyy/MM/dd");
        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = oFilename.substring(oFilename.lastIndexOf("."));
        String key = module + "/" + folder + "/" + fileName + fileExtension;
        //文件上传至阿里云
        ossClient.putObject(bucketName, key, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        //返回url地址
        return "https://" + bucketName + "." + endPoint + "/" + key;
    }

    /**
     * 删除讲师头像
     *
     * @param url
     */
    @Override
    public void removeFile(String url) {

        List<Oss> ossList = ossMapper.selectList(null);
        for (Oss oss : ossList) {
            endPoint = oss.getEndpoint();
            keyId = oss.getAccessKeyId();
            keySecret = oss.getAccessKey();
            bucketName = oss.getBucketName();
        }
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
        String host = "https://" + bucketName + "." + endPoint + "/";
        String objectName = url.substring(host.length());
        // 删除文件。
        ossClient.deleteObject(bucketName, objectName);
        // 关闭OSSClient。
        ossClient.shutdown();
    }
}


/**
 * 配置文件方式
 */
/*
    @Autowired
    private OssProperties ossProperties;
    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {
        String endpoint = ossProperties.getEndpoint();
        String keyid = ossProperties.getAccessKeyId();
        String keysecret = ossProperties.getAccessKey();
        String bucketname = ossProperties.getBucketName();
        //判断oss实例是否存在：如果不存在则创建，如果存在则获取
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        if (!ossClient.doesBucketExist(bucketname)) {
            //创建bucket
            ossClient.createBucket(bucketname);
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }
        //构建日期路径：avatar/2019/02/26/文件名
        String folder = new DateTime().toString("yyyy/MM/dd");
        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = oFilename.substring(oFilename.lastIndexOf("."));
        String key = module + "/" + folder + "/" + fileName + fileExtension;
        //文件上传至阿里云
        ossClient.putObject(ossProperties.getBucketName(), key, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        //返回url地址
        return "https://" + bucketname + "." + endpoint + "/" + key;
    }
*/
